package com.example.manage.controller;

import com.example.manage.bean.ResultBean;
import com.example.manage.bean.RoomUserBean;
import com.example.manage.bean.UserBean;
import com.example.manage.mapper.RoomUserMapper;
import com.example.manage.utils.MD5Util;
import com.example.manage.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/roomUser")
public class RoomUserController {


    @Autowired
    RoomUserMapper roomUserMapper;

    @RequestMapping("/addRoomUser")
    @ResponseBody
    public ResultBean addRoomUser(@RequestBody RoomUserBean roomUserBean) {
        RoomUserBean user = roomUserMapper.getRoomUserByPhone(roomUserBean.getPhone());
        if (user != null) {
            return ResultUtil.getFailBean("添加失败,手机号已存在");
        }
        String id = UUID.randomUUID().toString().replace("-", "");
        roomUserBean.setId(id);
        roomUserBean.setPwd(MD5Util.MD5("123456"));
        int count = roomUserMapper.addRoomUser(roomUserBean);
        return ResultUtil.getResultBean(count, "添加失败");
    }

    @RequestMapping("/login")
    @ResponseBody
    public ResultBean login(@RequestBody UserBean userBean) {
        RoomUserBean user = roomUserMapper.getRoomUserByPhone(userBean.getPhone());
        if (user == null) {
            return ResultUtil.getFailBean("无该用户");
        }
        if (user.getPwd().equals(userBean.getPwd())) {
            user.setPwd("");
            return ResultUtil.getSuccessBean(user);
        } else {
            return ResultUtil.getFailBean("密码不正确");
        }

    }


    @RequestMapping("/updatePwd")
    @ResponseBody
    public ResultBean updatePwd(@RequestBody RoomUserBean roomUserBean) {
        RoomUserBean user = roomUserMapper.getRoomUserByPhone(roomUserBean.getPhone());
        if (user == null) {
            return ResultUtil.getFailBean("无该用户");
        }

        if (user.getPwd().equals(roomUserBean.getPwd())) {
            roomUserBean.setuPwd(MD5Util.MD5(roomUserBean.getuPwd()));
            int count = roomUserMapper.updatePwd(roomUserBean);
            return ResultUtil.getResultBean(count, "修改失败");
        } else {
            return ResultUtil.getFailBean("原密码不正确");
        }

    }


    @RequestMapping("/deleteRoomUserById")
    @ResponseBody
    public ResultBean deleteRoomUserById(@RequestBody Map<String, String> params) {
        int count = roomUserMapper.deleteRoomUserById(params.get("id"));
        return ResultUtil.getResultBean(count, "删除失败");
    }

    @RequestMapping("/updateRoomUserById")
    @ResponseBody
    public ResultBean updateRoomUserById(@RequestBody RoomUserBean roomUserBean) {
        int count = roomUserMapper.updateRoomUserById(roomUserBean);
        return ResultUtil.getResultBean(count, "修改失败");
    }

    @RequestMapping("/queryRoomUser")
    @ResponseBody
    public ResultBean queryRoomUser() {
        List<RoomUserBean> roomUserBeans = roomUserMapper.queryRoomUser();
        return ResultUtil.getSuccessBean(roomUserBeans);
    }

    @RequestMapping("/queryRoomUserByCondition")
    @ResponseBody
    public ResultBean queryRoomUserByCondition(@RequestParam("condition") String condition) {
        List<RoomUserBean> roomUserBeans = roomUserMapper.queryRoomUserByCondition(condition);
        return ResultUtil.getSuccessBean(roomUserBeans);
    }


}
